package com.lin.mr.maizilaundry.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lin.mr.maizilaundry.R;
import com.lin.mr.maizilaundry.fragmentcloth.FragmentAnimation;
import com.lin.mr.maizilaundry.fragmentcloth.FragmentSpring;
import com.lin.mr.maizilaundry.fragmentcloth.FragmentContacts;
import com.lin.mr.maizilaundry.fragmentcloth.FragmentVolley;
import com.lin.mr.maizilaundry.myadapter.ClothAdapter;

import java.util.ArrayList;
import java.util.List;

public class ClothActivity extends FragmentActivity {
    private ImageView iv_back_cloth;
    private TextView tv_fragment_cloth;//春/秋装
    private TextView tv_fragment_contacts;//好友
    private TextView tv_fragment_animation;//动画
    private TextView tv_fragment_volley;//Volley
    private TextView cursor;
    private ViewPager viewPager;
    private List<Fragment> fragmentList;
    private int cursorIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cloth);
        initView();
        initCursor();//初始化滑块
        initViewPager();
    }


    private void initView() {
        tv_fragment_cloth = (TextView) findViewById(R.id.tv_fragment_cloth);
        tv_fragment_contacts = (TextView) findViewById(R.id.tv_fragment_contacts);
        tv_fragment_animation = (TextView) findViewById(R.id.tv_fragment_animation);
        tv_fragment_volley = (TextView) findViewById(R.id.tv_fragment_volley);
        tv_fragment_cloth.setOnClickListener(new TextListener(0));
        tv_fragment_contacts.setOnClickListener(new TextListener(1));
        tv_fragment_animation.setOnClickListener(new TextListener(2));
        tv_fragment_volley.setOnClickListener(new TextListener(3));
        iv_back_cloth = (ImageView) findViewById(R.id.iv_back_cloth);
        iv_back_cloth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //自定义选项卡的监听事件
    private class TextListener implements View.OnClickListener{
        private int viewPagerIndex = 0;

        //重写构造函数
        public TextListener(int i){
            viewPagerIndex = i;
        }

        @Override
        public void onClick(View v) {
            viewPager.setCurrentItem(viewPagerIndex);
        }
    }
    //初始化滑块
    private void initCursor() {
        cursor = (TextView) findViewById(R.id.fragment_cursor);
        //获取手机屏幕宽度
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        //获取滑块所在的父控件
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) cursor.getLayoutParams();
        //设置父控件的宽度，为屏幕的四分之一
        lp.width = dm.widthPixels/4;
        //设置滑块的宽度，即跟父控件的相同
        cursor.setLayoutParams(lp);
    }


    private void initViewPager() {
        fragmentList = new ArrayList<>();
        viewPager = (ViewPager) findViewById(R.id.cloth_activity_viewPager);
        FragmentSpring mSpring = new FragmentSpring();
        FragmentContacts mContacts = new FragmentContacts();
        FragmentAnimation mAnimation= new FragmentAnimation();
        FragmentVolley mVolley = new FragmentVolley();
        fragmentList.add(mSpring);
        fragmentList.add(mContacts);
        fragmentList.add(mAnimation);
        fragmentList.add(mVolley);
        viewPager.setAdapter(new ClothAdapter(getSupportFragmentManager(),fragmentList));
        viewPager.setCurrentItem(0);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            //页卡滑动时调用

            /**
             *
             * @param position:当前页面以及滑动后的页面
             * @param positionOffset:当前页面偏移的百分比
             * @param positionOffsetPixels:当前页面偏移的像素位置
             */
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.e("Tag","position:"+position);
                Log.e("Tag","positionOffset:"+positionOffset);
                Log.e("Tag","positionOffsetPixels:"+positionOffsetPixels);
                //获取滑块所在的父布局
                RelativeLayout.LayoutParams ll = (RelativeLayout.LayoutParams) cursor.getLayoutParams();
                if (cursorIndex == position){
                    //注意：position只有在滑块彻底划过某一个页卡时才会改变
                    ll.leftMargin = (int) (cursorIndex * cursor.getWidth() + positionOffset * cursor.getWidth());
                }else if(cursorIndex > position){
                    ll.leftMargin = (int) (cursorIndex * cursor.getWidth()- (1-positionOffset)*cursor.getWidth());
                }
//                else if (cursorIndex < position){
//                    ll.leftMargin = (int) (cursorIndex * cursor.getWidth() + (1-positionOffset)*cursor.getWidth());
//                }
                cursor.setLayoutParams(ll);
            }
            //页卡被选中时调用
            @Override
            public void onPageSelected(int position) {
                //页卡被选中时，使滑块和页卡所处的位置一致
                cursorIndex = position;
                //根据当前的页卡位置改变滑块颜色
                changeColor(position);
//                int i = cursorIndex + 1;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
    /**
     * 选项卡滑动时改变字体颜色
     */
    private void changeColor(int i){
        if (i==0){
            tv_fragment_cloth.setTextColor(getResources().getColor(R.color.color_main_blue));
            tv_fragment_contacts.setTextColor(getResources().getColor(R.color.color_text));
            tv_fragment_animation.setTextColor(getResources().getColor(R.color.color_text));
            tv_fragment_volley.setTextColor(getResources().getColor(R.color.color_text));
        }else if(i==1){
            tv_fragment_cloth.setTextColor(getResources().getColor(R.color.color_text));
            tv_fragment_contacts.setTextColor(getResources().getColor(R.color.color_main_blue));
            tv_fragment_animation.setTextColor(getResources().getColor(R.color.color_text));
            tv_fragment_volley.setTextColor(getResources().getColor(R.color.color_text));
        }else if (i==2){
            tv_fragment_cloth.setTextColor(getResources().getColor(R.color.color_text));
            tv_fragment_contacts.setTextColor(getResources().getColor(R.color.color_text));
            tv_fragment_animation.setTextColor(getResources().getColor(R.color.color_main_blue));
            tv_fragment_volley.setTextColor(getResources().getColor(R.color.color_text));
        }else if (i==3){
            tv_fragment_cloth.setTextColor(getResources().getColor(R.color.color_text));
            tv_fragment_contacts.setTextColor(getResources().getColor(R.color.color_text));
            tv_fragment_animation.setTextColor(getResources().getColor(R.color.color_text));
            tv_fragment_volley.setTextColor(getResources().getColor(R.color.color_main_blue));
        }
    }
}
